package com.xkdx.caipiao.open;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xkdx.caipiao.R;
import com.xkdx.caipiao.ThreadActivity;
import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.presistence.quert.OpenAwardNumQueryItemInfo;
import com.xkdx.caipiao.wiget.XListView;
import com.xkdx.caipiao.wiget.XListView.IXListViewListener;

public class OpenHistoryActivity extends ThreadActivity implements
		IXListViewListener {
	private ImageView back, tip;

	private TextView period, time;
	private String lottery_name;
	private XListView lv;
	private OpenHistoryQueryAction action;
	private OpenHistoryQueryModule module;
	private OpenHistoryQueryPresistence presistence;
	private OpenHistoryAdapter adapter;
	private String index = "1", size = "10";
	private List<OpenAwardNumQueryItemInfo> list = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.open_history_caipiao);
		lottery_name = getIntent().getStringExtra("name");
		list = new ArrayList<OpenAwardNumQueryItemInfo>();
		findView();
		startThread();

	}

	// 初始化listview
	private void findView() {
		tip = (ImageView) findViewById(R.id.im_open_today);

		lv = (XListView) findViewById(R.id.lv_open_history);
		lv.setFooterViewInVisible();
		lv.setPullRefreshEnable(true);
		lv.setXListViewListener(this);
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// Intent open_detail = new Intent(OpenHistoryActivity.this,
				// OpenActivity.class);
				// open_detail.putExtra("name",
				// list.get(position).getLotteryid());
				//
				// startActivity(open_detail);
			}
		});

	}

	class OpenHistoryAdapter extends BaseAdapter {

		@Override
		public int getCount() {

			return list.size();
		}

		@Override
		public Object getItem(int position) {

			return list.get(position);
		}

		@Override
		public long getItemId(int position) {

			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			View view;

			ViewHoler viewholer;
			if (convertView != null) {
				view = convertView;
				viewholer = (ViewHoler) view.getTag();
			} else {
				viewholer = new ViewHoler();
				view = View.inflate(getApplicationContext(),
						R.layout.open_history_item_caipiao, null);

				viewholer.period = (TextView) view.findViewById(R.id.periods);
				viewholer.time = (TextView) view.findViewById(R.id.time);
				viewholer.tip = (ImageView) view
						.findViewById(R.id.im_open_today);
				viewholer.ll_open = (LinearLayout) view
						.findViewById(R.id.ll_open);
				viewholer.ball1 = (Button) view.findViewById(R.id.ball1);
				viewholer.ball2 = (Button) view.findViewById(R.id.ball2);
				viewholer.ball3 = (Button) view.findViewById(R.id.ball3);
				viewholer.ball4 = (Button) view.findViewById(R.id.ball4);
				viewholer.ball5 = (Button) view.findViewById(R.id.ball5);
				viewholer.ball6 = (Button) view.findViewById(R.id.ball6);
				viewholer.ball7 = (Button) view.findViewById(R.id.ball7);
				viewholer.ball8 = (Button) view.findViewById(R.id.ball8);
				viewholer.ball9 = (Button) view.findViewById(R.id.ball9);
				view.setTag(viewholer);

			}

			viewholer.time.setText(list.get(position).getOpenawardtime());
			viewholer.period.setText("第" + list.get(position).getIssueid()
					+ "期");
			// if(position ==0){
			// viewholer.tip.setVisibility(View.VISIBLE);
			// }else{
			// viewholer.tip.setVisibility(View.GONE);
			// }

			if (lottery_name.equals("qxc")) {
				String[] str = list.get(position).getOpennum().split(",");
				viewholer.ball2.setVisibility(View.VISIBLE);
				viewholer.ball3.setVisibility(View.VISIBLE);
				viewholer.ball7.setVisibility(View.VISIBLE);
				viewholer.ball8.setVisibility(View.VISIBLE);
				viewholer.ball2.setText(str[0]);
				viewholer.ball3.setText(str[1]);
				viewholer.ball4.setText(str[2]);
				viewholer.ball5.setText(str[3]);
				viewholer.ball6.setText(str[4]);
				viewholer.ball7.setText(str[5]);
				viewholer.ball8.setText(str[6]);

			} else if (lottery_name.equals("dlt")) {
				String[] str = list.get(position).getOpennum()
						.replace("#", ",").split(",");
				viewholer.ball2.setVisibility(View.VISIBLE);
				viewholer.ball3.setVisibility(View.VISIBLE);
				viewholer.ball7.setVisibility(View.VISIBLE);
				viewholer.ball8.setVisibility(View.VISIBLE);
				viewholer.ball7.setBackgroundResource(R.drawable.ball_blue_caipiao);
				viewholer.ball8.setBackgroundResource(R.drawable.ball_blue_caipiao);
				viewholer.ball2.setText(str[0]);
				viewholer.ball3.setText(str[1]);
				viewholer.ball4.setText(str[2]);
				viewholer.ball5.setText(str[3]);
				viewholer.ball6.setText(str[4]);
				viewholer.ball7.setText(str[5]);
				viewholer.ball8.setText(str[6]);
			} else if (lottery_name.equals("pls")) {
				String[] str = list.get(position).getOpennum().split(",");
				//
				viewholer.ball4.setText(str[0]);
				viewholer.ball5.setText(str[1]);
				viewholer.ball6.setText(str[2]);
			} else {
				viewholer.ball3.setVisibility(View.VISIBLE);
				viewholer.ball7.setVisibility(View.VISIBLE);
				String[] str = list.get(position).getOpennum().split(",");
				viewholer.ball3.setText(str[0]);
				viewholer.ball4.setText(str[1]);
				viewholer.ball5.setText(str[2]);
				viewholer.ball6.setText(str[3]);
				viewholer.ball7.setText(str[4]);

			}

			return view;
		}

		class ViewHoler {
			TextView period, time;
			LinearLayout ll_open;
			Button ball1, ball2, ball3, ball4, ball5, ball6, ball7, ball8,
					ball9;
			ImageView tip;
		}

	}

	@Override
	public void showOnPost(HashMap<String, AbsModule> result) {
		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		if (result.containsKey("0")) {
			list = module.info.getList();

		}
		adapter = new OpenHistoryAdapter();
		lv.setAdapter(adapter);
		onLoad();

	}

	@Override
	public void startThread() {
		if (!(dataLoadDialog.isShowing())) {
			dataLoadDialog.show();
			dataLoadDialog.setContentView(R.layout.progressbar_caipiao);
		}
		action = new OpenHistoryQueryAction(index, size, lottery_name);
		module = new OpenHistoryQueryModule();
		presistence = new OpenHistoryQueryPresistence(this);
		presistence.setActitons(action);
		presistence.setModule(module);
		presistence.execute();

	}

	@Override
	public void onRefresh() {
		startThread();

	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub

	}

	private void onLoad() {
		lv.stopRefresh();
		lv.stopLoadMore();
		lv.setRefreshTime(getCurrentTime());
	}

	private String getCurrentTime() {
		SimpleDateFormat format = new SimpleDateFormat("MM月dd日 hh:mm:ss");
		Date date = new Date(System.currentTimeMillis());
		return format.format(date);
	}

	@Override
	public void onBackClick(View view) {
		finish();
	}

}